package cn.doctorpda.conference.ui.checked;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.ui.search.SearchCommonFragment;
import cn.doctorpda.conference.utils.ScreenUtils;
import cn.doctorpda.conference.utils.T;
import cn.doctorpda.conference.view.wheelview.adapter.ArrayWheelAdapter;
import cn.doctorpda.conference.view.wheelview.widget.WheelView;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 */
public class DateDialogFragment extends DialogFragment implements
        OnClickListener {

    private FragmentActivity mActivity;
    private WheelView yearPick, monthPick;
    private TextView tv_cancle, tv_ok;
    private OnDataPickListener listener;
    private Calendar c = Calendar.getInstance();
    private String year, month;


    public static DateDialogFragment newInstance(String year, String month) {
        DateDialogFragment fragment = new DateDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString("year", year);
        bundle.putString("month", month);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        year = getArguments().getString("year");
        month = getArguments().getString("month");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mActivity = getActivity();
        View inflate = inflater.inflate(R.layout.fragment_date, null);

        initWheel(inflate);
        getDialog().getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//        getDialog().setCanceledOnTouchOutside(false);
        return inflate;
    }

    public void setOnDataPickListener(OnDataPickListener listener) {
        this.listener = listener;
    }

    public interface OnDataPickListener {
        void setOnDataPickListener(String year, String month);
    }


    private void initWheel(View view) {

        yearPick = (WheelView) view.findViewById(R.id.year_wheelview);
        yearPick.setWheelAdapter(new ArrayWheelAdapter(getContext()));
        yearPick.setSkin(WheelView.Skin.Holo);
        yearPick.setWheelSize(3);//必须为奇数
        yearPick.setWheelData(createYears());
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.parseColor("#0288ce");
        style.textColor = Color.GRAY;
        if (ScreenUtils.getScreenHeight(mActivity) > 1080) {
            style.textSize = 30;
            style.selectedTextSize = 36;
        } else {
            style.textSize = 20;
            style.selectedTextSize = 24;
        }
        yearPick.setStyle(style);
//        Pattern p = Pattern.compile("[0-9]*");
        yearPick.setLoop(true);//放set前
        if (!year.isEmpty() && !month.isEmpty()) {
            yearPick.setSelection(Integer.valueOf(year) - 2014);
            T.showShort(getContext(), Integer.valueOf(year) + "现在" + c.get(Calendar.YEAR));
        } else {
            yearPick.setSelection(c.get(Calendar.YEAR) - 2014);
        }

        yearPick.setExtraText("年", Color.parseColor("#448AFF"), 30, 70);

        monthPick = (WheelView) view.findViewById(R.id.month_wheelview);
        monthPick.setWheelAdapter(new ArrayWheelAdapter(getContext()));
        monthPick.setWheelSize(3);
        monthPick.setSkin(WheelView.Skin.Holo);
        monthPick.setWheelData(createMonths());
        monthPick.setStyle(style);
        monthPick.setLoop(true);

        if (!year.isEmpty() && !month.isEmpty()) {
            monthPick.setSelection(Integer.valueOf(month));
        } else {
            monthPick.setSelection(c.get(Calendar.MONTH));
        }
        monthPick.setExtraText("月", Color.parseColor("#448AFF"), 30, 70);

        tv_cancle = (TextView) view.findViewById(R.id.btn_date_cancle);
        tv_ok = (TextView) view.findViewById(R.id.btn_date_ok);
        tv_ok.setOnClickListener(this);
        tv_cancle.setOnClickListener(this);

    }

    private ArrayList<String> createYears() {


        ArrayList<String> list = new ArrayList<String>();
        for (int i = 2014; i < c.get(Calendar.YEAR) + 2; i++) {
            if (i < 10) {
                list.add("0" + i);
            } else {
                list.add("" + i);
            }
        }
        return list;
    }

    private ArrayList<String> createMonths() {
        ArrayList<String> list = new ArrayList<String>();
        list.add("不限");
        for (int i = 1; i < 13; i++) {
            if (i < 10) {
                list.add("0" + i);
            } else {
                list.add("" + i);
            }
        }
        return list;
    }

    @Override
    public void onClick(View v) {
        dismiss();
        switch (v.getId()) {
            case R.id.btn_date_cancle:
                listener.setOnDataPickListener("", "");
//                T.showShort(getActivity(), "所有年份");
                break;
            case R.id.btn_date_ok:
                listener.setOnDataPickListener(yearPick.getSelectionItem().toString(), monthPick.getSelectionItem().toString());
//                Toast.makeText(getActivity(), "选择时间" + yearPick.getSelectionItem().toString() + monthPick.getSelectionItem().toString(), Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
    }


}
